<?php namespace Common\Model;
use Hdphp\Model\Model;
/**
 * 管理员模型
 */
class Admin extends Model
{
	protected $table = "admin";
	//自动验证
	protected $validate = array(
	         array('admin_username','required','请输入用户名',3,3),
	         array('admin_password','required','请输入密码',3,3),
	);
	/**
	 * 登录
	 */
	public function login()
	{		
		//自动验证
		if(!$this->create()) return false;
		//接收post的数据
		$admin_username = $_POST['admin_username'];
		$admin_password = md5($_POST['admin_password']);
		//验证用户名是否存在
		$data  = $this->where('admin_username',$admin_username)->first();
		if(!$data)
		{
			$this->error = "用户名不存在";
			return false;
		}
		if($admin_password != $data['admin_password'])
		{
			$this->error = "密码错误";
			return false;
		}		
		//能走到这一步说明登录成功了返回true
		$_SESSION['admin_username'] = $data['admin_username'];
		$_SESSION['admin_id'] = $data['admin_id'];
		return $this->where('admin_id',$_SESSION['admin_id'])->update(array('admin_logintime'=>time()));
	}
}
